package com.aaa.docter.service.Impl;

import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.core.util.RandomUtil;
import com.aaa.docter.Fegin.UserFeign;
import com.aaa.docter.dao.CareHistoryDao;
import com.aaa.docter.dao.HisPatientFileDao;
import com.aaa.docter.dao.PatientDao;
import com.aaa.docter.dao.RegistrationDao;
import com.aaa.docter.service.PatientService;
import com.aaa.docter.vo.Dvo;
import com.aaa.docter.vo.HisPatientVo;
import com.aaa.entity.*;
import com.aaa.util.IdWorker;
import com.aaa.util.WebGetUser;
import com.aaa.util.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PatientServiceImpl implements PatientService {
    @Autowired      //患者信息
    private PatientDao patientDao;
    @Autowired      //在门诊挂号里挂号
    private RegistrationDao registrationDao;
    @Autowired
    private UserFeign userFeign;

    @Autowired
    private HisPatientFileDao hisPatientFileDao;

    @Autowired
    private CareHistoryDao careHistoryDao;

    //开启全局事务
//    @Transactional
    @Override
    public Result<String> insertXinXi(HisPatient hisPatient, HisRegistration hisRegistration, Dvo a3) {
//根据当前患者的身份证号，查询是否在患者库里已经有信息
        QueryWrapper<HisPatient> wrapper = new QueryWrapper<>();
        System.out.println(hisPatient + "------------接受的参数");
//        指定生成患者id
        IdWorker idWorker = new IdWorker();

        wrapper.eq("id_card", hisPatient.getIdCard());
//根据身份证查询患者库是否有这个人
        HisPatient userInfo = patientDao.selectOne(wrapper);
        System.out.println("userInFo-------------------" + userInfo);
//        如果为空，则走这一步
        if (userInfo == null) {
            System.out.println("查无此人-------------------");
            long id = idWorker.nextId();
            String Pid = "HZ" + id;
            hisPatient.setPatientId(Pid);
            hisPatient.setCreateTime(new Date());
            patientDao.insert(hisPatient);
        }
//雪花算法
        long GH = idWorker.nextId();
        String Gid = "GH" + GH;

        System.out.println("最终添加前================= = " + hisPatient);
        System.out.println("最终添加前=============== = " + hisRegistration);
        hisRegistration.setPatientId(hisPatient.getPatientId());
        hisRegistration.setRegistrationId(Gid);
        hisRegistration.setPatientName(hisPatient.getName());

//        hisRegistration.setRegistrationId("1");
        String Phone = WebGetUser.UserUtil();
        User user = userFeign.queryUserByPhone(Long.valueOf(Phone));
        hisRegistration.setDoctorName(user.getUserName());
        hisRegistration.setUserId(user.getUserId());

//        时间类型转换
        LocalDateTime now = LocalDateTime.now();
        hisRegistration.setCreateTime(new Date());
        hisRegistration.setCreateBy(user.getUserName());

        hisRegistration.setSubsectionType(a3.getSchedulingType());
        hisRegistration.setDeptId(a3.getDeptId());
        hisRegistration.setRegistrationItemId(a3.getRegItemId());
        hisRegistration.setRegistrationAmount(a3.getRegItemAmount());
        hisRegistration.setVisitDate(a3.getVisitDate());
//        String s = RandomUtil.randomNumbers(10);
//        随机产生一个<200且>=100的int数
        int i = RandomUtil.randomInt(100, 900);
        hisRegistration.setRegistrationNumber(i);
        hisRegistration.setRegistrationStatus("0");
        int insert = registrationDao.insert(hisRegistration);

        if (insert > 0) {
            return new Result<String>(2000, "ok").setData(hisRegistration.getRegistrationId());
        }
        return Result.error();
    }


    @Override
    public Result<Map<String, Object>> patientAll(String patientId) {
        System.out.println("patientId ========== " + patientId);
        QueryWrapper<HisPatient> wrapper = new QueryWrapper<>();
        wrapper.eq("patient_id", patientId);
        HisPatient hisPatient = patientDao.selectOne(wrapper);

        QueryWrapper<HisPatientFile> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("patient_id", patientId);
        HisPatientFile hisPatientFile = hisPatientFileDao.selectOne(wrapper1);

        QueryWrapper<CareHistory> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("patient_id", patientId);
        List<CareHistory> careHistory = careHistoryDao.selectList(wrapper2);

        Map<String, Object> map = new HashMap<>();
        map.put("patientObj", hisPatient);
        map.put("patientFileObj", hisPatientFile);
        map.put("careHistoryObjList", careHistory);

        return new Result<>(2000, "患者档案历史查询", map);
    }
}
